package com.hidtechs.popfax.view;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hidtechs.popfax.R;
import com.hidtechs.popfax.controller.Constants;
import com.hidtechs.popfax.controller.Requestor;
import com.hidtechs.popfax.xml.ParsedDeleteDataSet;

public class EditContacts extends Activity implements Runnable,OnClickListener{


	Character class_type='f';
	String userName_shared_prefernece;
	String passWord_shared_prefernece;
	protected Handler handler;
	protected HttpURLConnection conn=null;
	protected ArrayList arrayOfObjectOfParsedData;
	public HashMap<String,String> parameterValues;
	private ProgressDialog progressDialog;
	private static String contact_id;
	EditText contactname;
	EditText contactnumber;
	Button save;
	private static String contact_name;
	private static String contact_number;
	public static String getContact_name() {
		return contact_name;
	}


	public static void setContact_name(String contact_name) {
		EditContacts.contact_name = contact_name;
	}


	public static String getContact_number() {
		return contact_number;
	}


	public static void setContact_number(String contact_number) {
		EditContacts.contact_number = contact_number;
	}





	public static String getContact_id() {
		return contact_id;
	}


	public static void setContact_id(String contact_id) {
		EditContacts.contact_id = contact_id;
	}


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editcontact);
		contactname=(EditText)findViewById(R.id.name);
		contactnumber=(EditText)findViewById(R.id.number);
		save=(Button)findViewById(R.id.save);
		save.setOnClickListener(this);
		//editContact(getContact_id());

	}

	public void onClick( View v) {
    	
		 contact_name = contactname.getText().toString();
        contact_number = contactnumber.getText().toString();                 
        if (contact_name.length()==0 || contact_number.length()==0)                
        {
            Toast.makeText(getBaseContext(), 
               Constants.EMPLTY_LOGIN_FIELDS_DATA, 
                Toast.LENGTH_SHORT).show();
        }else
        {
       //	 view = v;
        	progressDialog = ProgressDialog.show(this,Constants.DIALOG_WAIT_MSG,Constants.FAX_DELETION_MSG, true,false);
    		Thread thread = new Thread(this);
    		thread.start(); 
    		editHandlerMethod();
    	
        }
		   
       
	}
   

/*	public void editContact(String selected_contact_id,contact)
	{

		contact_id=selected_contact_id;

		progressDialog = ProgressDialog.show(this,Constants.DIALOG_WAIT_MSG,Constants.FAX_DELETION_MSG, true,false);
		Thread thread = new Thread(this);
		thread.start(); 
		deleteHandlerMethod();

	}*/

	public void run() {
		setMapValues();
		Requestor requestorForXMLParsing= new Requestor();
		arrayOfObjectOfParsedData=requestorForXMLParsing.requestData(class_type,parameterValues);
		handler.sendEmptyMessage(0);		
	}



	public void editHandlerMethod()
	{
		handler = new Handler() {

			public void handleMessage(Message msg) {
				progressDialog.dismiss();

				System.out.println("Handler");
				if(((ParsedDeleteDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_SUCCESS_CODE)
				{
					Intent tabIntent = new Intent(getApplicationContext(),com.hidtechs.popfax.view.TabWidgetController.class);
					startActivity(tabIntent);
				}
				if(((ParsedDeleteDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_FAILURE_CODE)
				{
					Toast.makeText(getApplicationContext(),Constants.LOGGED_IN_FAILED_MSG, Toast.LENGTH_SHORT).show();
					Intent tabIntent = new Intent(getApplicationContext(),com.hidtechs.popfax.view.TabWidgetController.class);
					startActivity(tabIntent);
				}

			}

		};

	}



	void setMapValues() {

		loadPreference(); 
		parameterValues = new HashMap();
		parameterValues.put(Constants.URL_ACTION, Constants.DELETE_CONTACT_ACTION);
		parameterValues.put(Constants.URL_USERNAME,userName_shared_prefernece);
		parameterValues.put(Constants.URL_PASSWORD,passWord_shared_prefernece);
		parameterValues.put(Constants.CONTACT_ID,getContact_id() );
		parameterValues.put(Constants.CONTACT_NAME,contact_name );
		parameterValues.put(Constants.CONTACT_NUMBER,contact_number );
	}



	protected void loadPreference()
	{
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences mySharedPreferences = getSharedPreferences("preferences", mode);
		userName_shared_prefernece= mySharedPreferences.getString("USER_NAME","UserName");
		System.out.println(userName_shared_prefernece);
		passWord_shared_prefernece = mySharedPreferences.getString("PASS_WORD","Password");
		System.out.println(passWord_shared_prefernece);
	}
}
